﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Kokomo.Environment;
using Kokomo.Environment.Documents;

namespace Kokomo.Environment.Documents
{
	/// <summary>
	/// Represents a <see cref="DetailViewModel"/> composed of tabs.
	/// </summary>
	/// <remarks>
	/// The view template displays a tab page for each item in
	/// <see cref="Tabs"/>.
	/// </remarks>
	[ViewType(typeof(TabbedDetailView))]
	public class TabbedDetailViewModel : DetailViewModel
	{
		/// <summary>
		/// Initializes a new instance of <see cref="TabbedDetailViewModel"/>.
		/// </summary>
		/// <param name="navigationContext">The navigation context.</param>
		/// <param name="content">The content of the tab.</param>
		/// <param name="heading"></param>
		public TabbedDetailViewModel(
			NavigationContext navigationContext,
			object content,
			UiString heading
			)
			: base(navigationContext, content, heading)
		{
			this.Tabs = new ObservableCollection<TabPageViewModel>();
			this.Tabs.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Tabs_CollectionChanged);
		}

		void Tabs_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
		{
			if (this.SelectedTab == null)
			{
				this.SelectedTab = this.Tabs.FirstOrDefault();
			}
		}

		private TabPageViewModel _selectedTab;
		/// <summary>
		/// Gets or sets the selected <see cref="TabPageViewModel"/>.
		/// </summary>
		public TabPageViewModel SelectedTab
		{
			get { return this._selectedTab; }
			set
			{
				this._selectedTab = value;
				this.OnPropertyChanged("SelectedTab");
			}
		}

		/// <summary>
		/// Gets the list of <see cref="TabPageViewModel"/> to display.
		/// </summary>
		public ObservableCollection<TabPageViewModel> Tabs { get; private set; }
	}
}
